<template>
  <div class="container">
    <div class="app-container">
      <div class="search">
        <el-form class="search-form">
          <el-form-item label="商品搜索：" label-width="120px">
            <el-input
              v-model="pageParams.skuName"
              placeholder="请输入"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="search-btn"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="searchSku"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="list">
        <div class="btns">
          <el-button
            type="primary"
            class="add-btn"
            icon="el-icon-circle-plus-outline"
            @click="addUser"
            >新建</el-button
          >
          <el-button class="setting-btn" @click="showSetDialog = true"
            >导入数据</el-button
          >
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column width="80px" label="序号">
            <template v-slot="{ row }"> {{ row.index }} </template>
          </el-table-column>
          <el-table-column prop="skuName" label="商品名称"> </el-table-column>
          <el-table-column label="商品图片" v-slot="{ row }">
            <template
              ><img
                style="width: 24px; height: 24px"
                :src="row.skuImage"
                alt=""
            /></template>
          </el-table-column>
          <el-table-column prop="brandName" label="品牌"> </el-table-column>
          <el-table-column prop="unit" label="规格"> </el-table-column>
          <el-table-column label="商品价格" v-slot="{ row }">
            <template>{{ row.price / 100 }}</template>
          </el-table-column>
          <el-table-column prop="skuClass.className" label="商品类型">
          </el-table-column>
          <el-table-column prop="createTime" label="创建日期" v-slot="{ row }">
            <template>{{ row.createTime.split("T").join(" ") }}</template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button type="text" @click="editSku(row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="space-between" style="margin-top: 20px">
          <span class="pageCount"
            >共{{ pageParams.totalCount }}条记录 第{{ pageParams.pageIndex }}/{{
              pageParams.totalPage
            }}页</span
          >
          <el-pagination
            layout="slot"
            :current-page.sync="pageParams.pageIndex"
            :page-size="pageParams.pagesize"
            :total="pageParams.totalPage"
          >
            <el-button
              type="primary"
              :disabled="pageParams.pageIndex === 1 ? true : false"
              class="pagi-btn"
              :class="{ disabled: pageParams.pageIndex === 1 }"
              @click="goPrev"
              >上一页</el-button
            >
            <el-button
              type="primary"
              :disabled="
                pageParams.pageIndex === pageParams.totalPage ? true : false
              "
              :class="{
                disabled: pageParams.pageIndex === pageParams.totalPage,
              }"
              class="pagi-btn"
              @click="goNext"
              >下一页</el-button
            >
          </el-pagination>
        </el-row>
      </div>
      <!-- 弹窗 -->
      <!-- 添加 -->
      <addSku
        v-if="showAddDialog"
        :title="title"
        :skuNow="skuNow"
        :isPrice="price"
        @searchSku="searchSku"
        :isShow.sync="showAddDialog"
      />
      <!-- 导入数据 -->
      <importFile
      v-if="showSetDialog"
      :isShow.sync="showSetDialog"
     />
    </div>
  </div>
</template>
<script>
import { searchSkuApi } from "@/api/sku";
import addSku from "@/views/sku/components/addSku.vue";
import importFile from "@/views/sku/components/importFile.vue";
export default {
  components: {
    addSku,
    importFile
  },
  data() {
    return {
      tableData: [],
      title: "",
      skuNow: {},
      price: 0,
      showAddDialog: false,
      showSetDialog: false,
      setForm: {},
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
        skuName: "",
      },
    };
  },
  created() {
    this.searchSku();
  },
  methods: {
    // 搜索
    async searchSku() {
      const res = await searchSkuApi({
        ...this.pageParams,
      });
      this.tableData = [...res.currentPageRecords];
      this.pageParams.pageIndex = +res.pageIndex;
      this.pageParams.pageSize = +res.pageSize;
      this.pageParams.totalPage = +res.totalPage;
      this.pageParams.totalCount = +res.totalCount;

      // 序号
      let pageNow = this.pageParams.pageIndex;
      this.tableData.forEach((item, index) => {
        this.$set(
          item,
          "index",
          pageNow > 1 ? (pageNow - 1) * 10 + index + 1 : index + 1
        );
      });
    },

    addUser() {
      this.title = "新增商品";
      this.showAddDialog = true;
    },
    async editSku(row) {
      console.log(row);
      this.price = row.price / 100;
      this.title = "修改商品";
      this.skuNow = row;
      this.showAddDialog = true;
    },
    

    // 上一页
    goPrev() {
      this.pageParams.pageIndex--;
      this.searchSku();
    },
    // 下一页
    goNext() {
      this.pageParams.pageIndex++;
      this.searchSku();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #f8fafd;
  padding: 20px;
  .search {
    background-color: #fff;
    display: flex;
    height: 64px;
    align-items: center;
    padding-top: 23px;
    margin-bottom: 20px;
    .search-form {
      display: flex;
      align-items: center;
      .search-btn {
        background-color: #5f84ff;
        border: none;
        margin-left: 15px;
        font-size: 16px;
        height: 36px;
        width: 80px;
        text-align: center;
        padding: 0;
      }
    }
  }
  .list {
    padding: 20px;
    background-color: #fff;

    .btns {
      margin-bottom: 15px;
    }
    .add-btn {
      width: 80px;
      text-align: center;
      padding: 12px 0px;
      background-color: #ff8035;
      border: none;
      font-size: 14px;
    }
    .setting-btn {
      border: none;
      background-color: #fbf4f0;
      color: #716762;
      width: 80px;
      text-align: center;
      padding: 12px 0px;
    }
    .pageCount {
      color: #dbdfe5;
      margin-left: 10px;
    }
  }
  .pagi-btn {
    background-color: #d5ddf8;
    color: #606266;
    width: 70px;
    height: 32px;
    text-align: center;
  }
  .disabled {
    background-color: #eceff8;
    color: #d2d7dd;
  }
  .del {
    color: #ff5a5a;
  }
}
.vmImage {
  width: 24px;
  height: 24px;
  border-radius: 12px;
}
</style>
